package org.jetlinks.community.mstn.aop;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hswebframework.web.aop.MethodInterceptorHolder;
import org.hswebframework.web.logging.LoggerDefine;
import org.hswebframework.web.logging.aop.AccessLoggerParser;
import org.jetlinks.community.mstn.annotation.Tenant;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;

@Component
public class TenantAccessLoggerParser implements AccessLoggerParser {
    @Override
    public boolean support(Class<?> aClass, Method method) {
        Tenant api = (Tenant) AnnotationUtils.findAnnotation(aClass, Tenant.class);
        Tenant operation = (Tenant)AnnotationUtils.findAnnotation(method, Tenant.class);
        return api != null || operation != null;

    }

    @Override
    public LoggerDefine parse(MethodInterceptorHolder holder) {
        Tenant api = (Tenant)holder.findAnnotation(Tenant.class);
        Tenant operation = (Tenant) AnnotatedElementUtils.findMergedAnnotation(holder.getMethod(), Tenant.class);
        String action = "";
        if (api != null) {
            action = action.concat(api.title());
        }

        if (null != operation) {
            action = StringUtils.isEmpty(action) ? operation.title() : action + "-" + operation.title();
        }

        return new LoggerDefine(action, "test");
    }
}
